﻿using iCodeStudio.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace iCodeStudio.Controllers
{
     
    public class UserManageController : BaseController
    {
        
        public ActionResult UserList(int? currentPage, int? numPerPage)
        {
            var pageIndex = currentPage.HasValue ? currentPage.Value - 1 : 0;
            var pageSize = numPerPage.HasValue && numPerPage.Value > 0 ? numPerPage.Value : 20;

            IList<iCode_User> userList = ICode_UserServices.GetAlliCode_User();
            userList = userList.Where(obj => !obj.iCode_User_Account.Equals("admin")).ToList();
            string userName = Request["userName"] ?? string.Empty;
            
            if (!string.IsNullOrEmpty(userName))
            {
                userList = userList.Where(obj => obj.iCode_User_Name.Contains(userName) || obj.iCode_User_Account.Contains(userName)).ToList();
            }
            userList = userList.Skip(pageSize * pageIndex).Take(pageSize).ToList();

            ViewBag.TotalCount = userList.Count;
            ViewBag.NumPerPage = pageSize;
            ViewBag.PageNum = pageIndex + 1;
            ViewBag.UserName = userName;
            return View(userList);
        }

        public ActionResult AddUser(string id)
        {
            iCode_User user = new iCode_User();
            if (!string.IsNullOrEmpty(id))
            {
                user = ICode_UserServices.GetUserByid(Convert.ToInt32(id));
            }
            return View(user);
        }

        public ActionResult EditUser(string id)
        {
            return RedirectToAction("AddUser/" + id);
        }

        public ActionResult DeleteUser(string ids)
        {
            string jsonResult = "";
            try
            {
                if (ids.Split(',').Length > 0)
                {
                    for (int i = 0; i < ids.Split(',').Length; i++)
                    {
                        ICode_UserServices.DeleteUser(ICode_UserServices.GetUserByid(Convert.ToInt32(ids.Split(',')[i])));
                    }
                }
                else
                {
                    ICode_UserServices.DeleteUser(ICode_UserServices.GetUserByid(Convert.ToInt32(ids)));
                }
                jsonResult = doSuccess("userListPage", false);
            }
            catch (Exception ex)
            {

                jsonResult = doFail(ex.Message);
            }
            return Json(jsonResult);
        }

        [HttpPost]
        public ActionResult AddUser(iCode_User user)
        {
            string jsonResult = "";
            try
            {
                if (user.iCode_User_ID == 0)
                {
                    ICode_UserServices.AddUser(user);
                }
                else
                {
                    ICode_UserServices.UpdateUser(user);
                }
                jsonResult = doSuccess("userListPage", true);

            }
            catch (Exception ex)
            {
                jsonResult = doFail(ex.Message);

            }
            return Json(jsonResult);

        }



        public string doSuccess(string navTabId, bool closeWindow)
        {
            if (closeWindow = true)
            {
                return "{\"statusCode\":\"200\", \"message\":\"操作成功\", \"navTabId\":\"" + navTabId + "\", \"forwardUrl\":\"\", \"callbackType\":\"closeCurrent\", \"rel\":\"\"}";
                //return "{\"statusCode\":\"200\", \"message\":\"" + resutlDesc + "\"}";
            }
            else
            {
                return "{\"statusCode\":\"200\", \"message\":\"操作成功\", \"navTabId\":\"" + navTabId + "\", \"forwardUrl\":\"\", \"callbackType\":\"\", \"rel\":\"\"}";
            }
        }
        public string doFail(string failReson)//批量删除会自动刷新所在的navTab。 
        {
            return "{\"statusCode\":\"300\", \"message\":\"" + failReson + "\"}";
            // return "{\"statusCode\":\"200\", \"message\":\"" + resutlDesc + "\",\"navTabId\":\"" + navTabId + "\",\"callbackType\":\"closeCurrent\"}";
        }
        public string failure(string resutlDesc)
        {
            return "{\"statusCode\":\"301\", \"message\":\"会话超时\"}";
            // return "{\"statusCode\":\"300\", \"message\":\"" + resutlDesc + "\"}";
        }
    }
}